<template>
  {{ form.timeRange1 }}
  <el-form :model="form" :rules="rules" ref="formRef" label-width="120px">
    <!-- 第一个时间范围 -->
    <el-form-item label="时间范围" prop="timeRange1">
      <MultipleTime v-model="form.timeRange1" />
    </el-form-item>

    <!-- 提交按钮 -->
    <el-form-item>
      <el-button type="primary" @click="submitForm">提交</el-button>
      <el-button @click="resetForm">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import MultipleTime from '../components/MultipleTimeRange.vue'
import type { ElForm } from 'element-plus'

const formRef = ref<InstanceType<typeof ElForm>>()

// 表单数据
const form = reactive({
  timeRange1: [
    {
      startTime: new Date('Thu Aug 22 2024 00:00:00 GMT+0800').toTimeString(),
      endTime: new Date().toLocaleTimeString(),
    },
    {
      startTime: '1900-01-01 00:00:00',
      endTime: '1900-01-01 23:59:59',
    },
    {
      startTime: '1900-01-01 00:00:00',
      endTime: '1900-01-01 23:59:59',
    },
  ], // 时间范围 1，默认为空数组
})

// 表单验证规则
const rules = reactive({
  timeRange1: [
    {
      required: true,
      message: '请选择时间范围 1',
      trigger: 'change',
    },
  ],
  timeRange2: [
    {
      required: true,
      message: '请选择时间范围 2',
      trigger: 'change',
    },
  ],
})

// 提交表单
const submitForm = () => {
  formRef.value?.validate((valid) => {
    if (valid) {
      console.log('表单数据：', form)
      alert('表单提交成功！')
    } else {
      console.log('表单验证失败！')
      // return false
    }
  })
}

// 重置表单
const resetForm = () => {
  formRef.value?.resetFields()
}
</script>
